#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e6+5;const ll inf=1e18;
priority_queue<pair<ll,int> >q;
ll dp[N],t2;int t,n,a[N];
void work(){
	scanf("%d",&n);while(!q.empty())q.pop();t2=0;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=0;i<N;i++)dp[i]=-inf;dp[0]=0;q.push(pair<ll,int>{0,0});
	for(int i=2;i<=n;i++){
		while(!q.empty()&&q.top().second==a[i])q.pop();
		ll mx=dp[a[i]]+a[i]+t2;
		if(!q.empty())mx=max(mx,q.top().first+t2);t2+=(a[i]==a[i-1]?a[i]:0);
		dp[a[i-1]]=max(dp[a[i]],mx-t2);q.push(pair<ll,int>{dp[a[i-1]],a[i-1]});
	}ll ans=0;
	for(int i=0;i<N;i++)ans=max(ans,dp[i]+t2);
	printf("%lld\n",ans);
}
int main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	scanf("%d",&t);
	while(t--)work();
	return 0;
}